/*
 * create by straysnail
 * 2022/2/26
 */
#define INA 7  //定义引脚控制左边电机方向
#define ENA 6  //控制左边电机速度
#define INB 8  //右边电机方向
#define ENB 5  //右边电机速度

void setup() {
  pinMode(INA, OUTPUT);  //设置控制电机的引脚为输出模式
  pinMode(ENA, OUTPUT);
  pinMode(INB, OUTPUT);
  pinMode(ENB, OUTPUT);
}

void loop() {
  car_forward();  //前进
  delay(2000);    //2s
  car_back();     //后退
  delay(2000);    //2s
  car_left();     //左旋转
  delay(2000);    //2s
  car_right();    //右旋转
  delay(2000);    //2s
  car_stop();     //停止
  delay(1000);    //1s
}

//小蜗牛前进
void car_forward()
{
  digitalWrite(INA, HIGH);  //输出高电平，控制左边电机正转
  analogWrite(ENA, 100);    //PWM输出，控制左边电机速度
  digitalWrite(INB, HIGH);  //右边电机正转
  analogWrite(ENB, 100);    //右边电机速度
}
//小蜗牛后退
void car_back()
{
  digitalWrite(INA, LOW);  //左边反转
  analogWrite(ENA, 100);   //左边速度
  digitalWrite(INB, LOW);  //右边反转
  analogWrite(ENB, 100);   //右边速度
}
//小蜗牛左旋转
void car_left()
{
  digitalWrite(INA, LOW);
  analogWrite(ENA, 100);
  digitalWrite(INB, HIGH);
  analogWrite(ENB, 100);
}
//小蜗牛右旋转
void car_right()
{
  digitalWrite(INA, HIGH);
  analogWrite(ENA, 100);
  digitalWrite(INB, LOW);
  analogWrite(ENB, 100);
}
//停止
void car_stop()
{
  digitalWrite(INA, HIGH);
  analogWrite(ENA, 0);
  digitalWrite(INB, HIGH);
  analogWrite(ENB, 0);
}
